package work;

import java.util.Random;
import java.util.Scanner;

public class LoginAndRegister2 {
    private static String registerUsername;

    private static int registerPassword;

    private static int registerCardNumber;

    private static boolean isLoggedIn;

    public static void menu() {
        System.out.println("******欢迎进入奖客富翁系统******");
        System.out.println("          1. 注册               ");//register
        System.out.println("          2. 登录               ");//login
        System.out.println("          3. 抽奖               ");//lottery
        System.out.println("*****************************");
        System.out.println("请选择菜单:");
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String choice = "y";
        while(choice.equals("y"))
        {
            menu();
            int input = sc.nextInt();
            switch (input){
                case 1:{
                    register(sc);
                    break;
                }
                case 2:{
                    login(sc);
                    break;
                }
                case 3:{
                    lottery(sc);
                    break;
                }
                default:
                    System.out.println("您输入的值错误,请重新输入");
            }
            System.out.println("继续吗:(y/n):");
            choice = sc.next();
        }
        System.out.println("系统退出,谢谢使用!");

    }

    public static void register(Scanner sc){
        System.out.println("[奖客富翁系统>注册]");
        System.out.println("请填写个人注册信息");
        System.out.println("用户名:");
        registerUsername = sc.next();
        System.out.println("密码(6位数)");
        registerPassword = sc.nextInt();
        System.out.println("注册成功,请记好您的会员卡号");
        registerCardNumber = random();
        System.out.println("用户名    密码    会员卡号");
        System.out.println(registerUsername +"   " + registerPassword +"   " + registerCardNumber);
    }

    public static void login(Scanner sc){
        System.out.println("[奖客富翁系统>登录]");
        System.out.println("请输入您的用户名:");
        String inputUserName = sc.next();
        System.out.println("请输入密码:");
        int inputPassword = sc.nextInt();
        if((inputPassword == registerPassword) && (inputUserName.equals(registerUsername))){
            System.out.println("欢迎您:" + registerUsername);
            isLoggedIn = true;
        }else{
            System.out.println("您的用户名或密码输入错误,请重新登录");
        }
    }

    public static void lottery(Scanner sc){
        boolean isWinning = false;
        if(!isLoggedIn) {
            System.out.println("您还未登录,请先登录");
            return;//相当于执行到这里,方法就结束了
        }
        System.out.println("奖客富翁系统>抽奖");
        int[] arr = new int[5];
        for (int i = 0; i < 5; i++) {
            arr[i] = random();
        }
        System.out.println("本日的幸运数字为 " + arr[0] + " " + arr[1] + " " + arr[3] + " " + arr[4] + " " + arr[2]);
        System.out.println("您的幸运数字为:" + registerCardNumber);
        for (int i = 0; i < 5; i++) {
            if (arr[i] == registerCardNumber) {
                isWinning = true;
                break;
            }
        }
        System.out.println(isWinning?"恭喜您,成为本日的幸运会员":"抱歉,您不是本日的幸运会员");
    }


    public static int random(){
        Random random = new Random();
        int num = random.nextInt(9000) + 1000;
        return num;
    }
}

